package com.dhn.controller;

import com.dhn.entity.Patient;
import com.dhn.service.PatientService;
import com.dhn.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class PatientController {
    @Autowired
    private PatientService patientService;
    @RequestMapping("getPatients")
    @ResponseBody
    public Object getPatients(Patient patient){
        int total = patientService.getTotal(patient);
        patient.setPage((patient.getPage()-1)*patient.getLimit());
        List<Patient> list = patientService.getPatients(patient);
        return Result.toClient("0","",total,list);
    }


    @RequestMapping("delPatient")
    @ResponseBody
    public Object delPatient(int id){
        int n = patientService.delPatientById(id);
        if(n > 0){
            return Result.toClient("0","删除成功");
        }else{
            return Result.toClient("1","删除失败");
        }
    }

    @RequestMapping("addPatient")
    @ResponseBody
    public Object addPatient(Patient patient){
        int n = patientService.addPatient(patient);
        if(n > 0){
            return Result.toClient("0","添加成功");
        }else{
            return Result.toClient("1","添加失败");
        }
    }

    @RequestMapping("updatePatient")
    @ResponseBody
    public Object updatePatient(Patient patient){
        int n = patientService.updatePatient(patient);
        if(n > 0){
            return Result.toClient("0","修改成功");
        }else{
            return Result.toClient("1","修改失败");
        }
    }
}
